﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
using System.IO;
namespace Web
{
    public partial class SelectPic : System.Web.UI.Page
    {
        public static Module_Page Pages = new Module_Page();
        private int _subjectid=0;
        private string _keyword = "";
        public int editid = 0;
        private static UserLinkExtendInfo uinfo = new UserLinkExtendInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserValidate.CheckLogin(this.Request, "QuesAdd"))
            {
                uinfo.Id = 0;
            }
            else if (UserValidate.TeacherCheckLogin(this.Request))
            {
                uinfo = UserValidate.TeacherGetInfo(this.Request);
            }
            else
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }

            if (Request.QueryString["page"] != null && Request.QueryString["page"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["page"]))
                {
                    Pages.PageNum = int.Parse(Request.QueryString["page"]);
                }
            }

            if (Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != string.Empty)
            {
                    _keyword = Request.QueryString["keyword"];
            }
            if (Request.QueryString["subjectid"] != null && Request.QueryString["subjectid"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["subjectid"]))
                {
                    _subjectid = int.Parse(Request.QueryString["subjectid"]);
                }
            }
            if (!IsPostBack)
            {
                LoadSubjectList();
                LoadList(_subjectid, _keyword);
                if (editid > 0)
                {
                    FilesInfo finfo = new FilesInfo();
                    finfo = Files.GetT1(editid);
                    if (uinfo.Id > 0 && finfo.UserId != uinfo.Id)
                    {
                        editid = 0;
                        Utility.ShowJsAlert("您不能编辑公共素材", this);
                        return;
                    }
                    Name.Text = finfo.Name;
                    SubjectIdList.SelectedValue = finfo.Subject.ToString();
                }
            }
        }

        protected void LoadList(int subjectid,string keyword)
        {
            Pages.AllCount = Files.GetCount(subjectid, keyword, uinfo.Id);
            Pages.PageSize = 4;
            Pages.PageName = "SelectPic.aspx?subjectid=" + subjectid + "&keyword=" + keyword + "&page=";
            Pages.Compute();
            PageLabel.Text = Pages.GetPage();
            IList<FilesInfo> filesInfo = Files.GetList(subjectid, keyword, uinfo.Id, Pages.First, Pages.Max);
            FilesList.DataSource = filesInfo;
            FilesList.DataBind();
        }

        protected void LoadSubjectList()
        {
            IList<SubjectInfo> subjectInfo = Subject.GetSubjectInfos();
            SubjectList.Items.Clear();
            SubjectList.Items.Add(new ListItem("按课程查看", "all"));
            SubjectList.Items.Add(new ListItem("查看全部", "0"));
            SubjectIdList.Items.Add(new ListItem("不分类", "0"));
            foreach (SubjectInfo info in subjectInfo)
            {
                SubjectList.Items.Add(new ListItem(info.Name, info.Id.ToString()));
                SubjectIdList.Items.Add(new ListItem(info.Name, info.Id.ToString()));
            }
        }

        protected void SubjectList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SubjectList.SelectedValue != "" || SubjectList.SelectedValue == "all")
            {
                if (SubjectList.SelectedValue == "0")
                {
                    Response.Redirect("SelectPic.aspx");
                }
                else
                {
                    Response.Redirect("SelectPic.aspx?subjectid=" + SubjectList.SelectedValue);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Keyword.Text.Trim() != "")
            {
                Response.Redirect("SelectPic.aspx?keyword=" + Keyword.Text.Trim());
            }
        }

        protected void save_Click(object sender, EventArgs e)
        {
            if (Name.Text.Trim() == "")
            {
                Utility.ShowJsAlert("名称不能为空", this);
                return;
            }
            //save.Enabled = false;
            FilesInfo finfo = new FilesInfo();

            if (editid > 0)
            {
                finfo = Files.GetT1(editid);
                finfo.Name = Name.Text;
                finfo.Subject = int.Parse(SubjectIdList.SelectedValue.Trim());
                Files.Edit(finfo);
                editid = 0;
                Response.Redirect(Pages.PageName + Pages.PageNum);
            }
            else
            {
                try
                {
                    if (FileUpload1.HasFile)
                    {

                        finfo.Name = Name.Text;
                        finfo.Subject = int.Parse(SubjectIdList.SelectedValue.Trim());
                        Module_File mf = new Module_File();
                        finfo.FileExt = Path.GetExtension(FileUpload1.FileName);
                        if (!mf.CheckExt(finfo.FileExt))
                        {
                            Utility.ShowJsAlert("您上传的文件类型不在允许范围内", this);
                            return;
                        }
                        finfo.FileSize = FileUpload1.PostedFile.ContentLength.ToString();
                        if (!mf.CheckFileSize(long.Parse(finfo.FileSize)))
                        {
                            Utility.ShowJsAlert("您上传的文件过大，最大上传：" + (mf.MaxSize / 1024).ToString() + "kb", this);
                            return;
                        }
                        save.Enabled = false;
                        finfo.FileName = mf.GetFileName(finfo.FileExt);
                        finfo.Patch = mf.GetDirName();
                        finfo.UserId = uinfo.Id;
                        mf.CreateUpfileDirectory(Server.MapPath(finfo.Patch));
                        FileUpload1.PostedFile.SaveAs(Server.MapPath(finfo.Patch + finfo.FileName));
                        finfo.Minipic = finfo.FileName.Replace(finfo.FileExt, "_s" + finfo.FileExt);
                        ImageLibrary.Basic im = new ImageLibrary.Basic();
                        im.reimgsize(Server.MapPath(finfo.Patch + finfo.FileName), Server.MapPath(finfo.Patch + finfo.Minipic), 120, 90);
                        Files.Add(finfo);
                        Utility.ShowJs("info('" + finfo.Patch + finfo.FileName + "');", this);
                        return;
                    }
                    else
                    {
                        Utility.ShowJsAlert("必须选择上传文件", this);
                        return;
                    }
                }
                catch
                {
                    Utility.ShowJsAlert("上传失败", this);
                    return;
                }
            }
        }
    }
}
